﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;

namespace KiraSh.ResumeTracker
{
    // MAIN WINDOW, LOADED THE FIRST
    public partial class frmMainWindow : Form
    {
        // list of all the profiles
        private List<Company> Companies = new List<Company>();
        private List<string> Companies_copy = new List<string>();
        // contains helper methods for xml
        private XMLDealer xml = new XMLDealer();
        // the form was closed
        private bool doClose = false;
        // fonts
        private Font boldFont = new System.Drawing.Font("Segoe UI", 10, FontStyle.Bold);
        private Font normalFont = new System.Drawing.Font("Segoe UI", 10);

        public frmMainWindow()
        {
            InitializeComponent();
            // update the name of the program
            CurrentContext.Name = ((AssemblyTitleAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false)[0]).Title;
            // create selecvt profile form
            frmSelectProfile frm = new frmSelectProfile();
            frm.form = this;
            // if the select profile was aborted then close the program
            if (frm.ShowDialog(this) == DialogResult.Abort)
            {
                doClose = true;
            }
            else
            {
                this.Text = CurrentContext.Name + " | Profile: " + CurrentContext.Profile.Name;
                // load companies for the current profile
                LoadCompanies();
            }
        }

        // add new company
        private void btnNewCompany_Click(object sender, EventArgs e)
        {
            // company name
            string selection = txtNewCompany.Text.Trim();
            try
            {
                // create new company and save it to disk
                CompanyManager.CreateNewCompany(selection);
                CompanyManager.SaveToDisk();
                LoadCompanies();
                txtNewCompany.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // load list of companies from xml
        private void LoadCompanies()
        {
            // load companies
            CompanyManager.LoadFromDisk();
            Companies = new List<Company>(CompanyManager.Companies);
            // sort companies alphabetically
            Companies.Sort();
            // clear the panel first
            flowLayoutPanel1.Controls.Clear();
            // update the drop box and a copy of the profiles list to eliminate duplicates
            foreach (Company cmp in Companies)
            {
                Companies_copy.Add(cmp.Name);
                // add a label
                Label label = new Label();
                label.AutoSize = true;
                label.Name = cmp.Name;
                label.Text = cmp.Name;
                label.Tag = cmp;
                label.Font = normalFont;
                label.Padding = new System.Windows.Forms.Padding(2, 3, 2, 3);

                label.Cursor = Cursors.Hand;
                label.MouseLeave += new EventHandler(label_MouseLeave);
                label.MouseEnter += new EventHandler(label_MouseEnter);
                label.Click += new EventHandler(label_Click);
                flowLayoutPanel1.Controls.Add(label);
            }
        }

        // change font color to black
        void label_MouseLeave(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            label.ForeColor = Color.Black;
            label.Font = normalFont;
        }

        // change font color to orange on mouse over
        void label_MouseEnter(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            label.ForeColor = Color.OrangeRed;
            label.Font = boldFont;
        }

        // load the list of application if the comapny name was clicked
        void label_Click(object sender, EventArgs e)
        {
            // update the company selected
            CurrentContext.Company = (Company) ((Label)sender).Tag;
            
            // load the form that displays all the applications
            frmDisplayJobs.ShowForm();
        }

        private void frmMainWindow_Load(object sender, EventArgs e)
        {
            // close if the profile selection form was aborted
            if (doClose)
            {
                this.Close();
                this.Dispose();
            }
        }

        // add master resume button functionality
        private void btnAddMaster_Click(object sender, EventArgs e)
        {
            frmAddMaster.showForm();
        }

        // add about box button functionality
        private void tlsBtnAbout_Click(object sender, EventArgs e)
        {
            bxAbout.showForm();
        }

        private void btnEmailSettings_Click(object sender, EventArgs e)
        {
            frmEmailSettings.ShowForm();
        }
    }
}
